/**
 * Created by sunyongqi on 2017/7/17.
 */
var socket = io(window.location.origin);

const getDate = function(){
    var _targetDate = new Date();
    const Y = _targetDate.getFullYear();
    const M = _targetDate.getMonth()>8?(_targetDate.getMonth()*1+1):('0' + (_targetDate.getMonth()*1+1));
    const D = _targetDate.getDate()>9?_targetDate.getDate():('0' + _targetDate.getDate());
    const h = _targetDate.getHours()>9?_targetDate.getHours():('0' + _targetDate.getHours());
    const m = _targetDate.getMinutes()>9?_targetDate.getMinutes():('0' + _targetDate.getMinutes());
    const s = _targetDate.getSeconds()>9?_targetDate.getSeconds():('0' + _targetDate.getSeconds());
    return Y+"-"+M+"-"+D+" "+h+":"+m+":"+s;
}
socket.on('msg',function (msg) {
    console.log('get msg',msg);
    if(msg && msg.message){
        const msgList = that.state.msgList?that.state.msgList:[];
        msgList.push(msg.message);
        that.setState({
            msgList:msgList
        });
    }
});
socket.on('online',function (msg) {
    console.log('online',msg);
    if(msg.username){
        message.warning(msg.username+'上线啦 (y*~*y)',2);
    }
    that.setState({
        numOnlineUsers: msg.numUsers
    });
});
socket.on('offline',function (msg) {
    console.log('offline',msg);
    if(msg.username) {
        message.warning(msg.username + "已下线 (|||-'_'-) ", 2);
    }
    that.setState({
        numOnlineUsers: msg.numUsers
    });
});
socket.on('errors',function (msg) {
    console.log('errors',msg);
    message.error(msg.infos,2);
    if(msg.code==2001){
        that.setState({
            showLogin:true,
            userName:''
        });
    }
});
socket.emit('login',userName);